<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Course extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(1);
		$this->load->model('Candidate_course_model', 'course');
		$this->lang->load('candidate/course', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$course_list = $this->course->get(array('idcandidate' => $this->session->userdata['idprofile']));
				
		$grid_course = array();
        
        foreach ($course_list->result() as $row)
		{
			$grid_course[] = '<div class="row"><span><span class="gtitle">' . $row->levelname . ' - ' . $row->typename . ' (' . $row->statusname . ')</span>'
			. '<br/><span class="gdesc">'.$row->institution.' - ' . format_date($row->startdate, true) . ($row->finishdate ? ' &agrave; ' . format_date($row->finishdate, true) : '') .'
			<br/>'.($row->statename != '' ? $row->statename.' / ' : '').$row->countryname.'</span>
			</span>
			<div class="gtool"><a onclick="CandidateCourse.editCourse('.$row->id.');">'.$this->lang->line('global_update').'</a>
			 &middot; <a onclick="CandidateCourse.deleteCourse('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
		}
		
		if (!$grid_course) {
			 $data['grid_course'] = '<center>'.$this->lang->line('course_noresults').'</center>';
		} else {
			 $data['grid_course'] = implode('<hr/>', $grid_course);
		}
		
		exitHTML($data['grid_course']);
	}
	
	public function insert()
	{
		$data = $_POST;
		
		if (missing_field($data, array('idlevel', 'idcoursetype', 'idstatus', 'startdate', 'institution', 'idcountry')))
		{
			formError($this->lang->line('course_missingfield'), true);
		}
		
		if (!valid_date($data['startdate']))
		{
			formError($this->lang->line('course_invalidstartdate'), true);
		} else {
			$data['startdate'] = format_date($data['startdate']);
		}
		
		if ($data['idstatus'] == 1 && isset($data['finishdate']) && $data['finishdate'] == '')
		{
			formError($this->lang->line('course_missingfinishdate'), true);
		}
		
		if (isset($data['finishdate']) && $data['finishdate'] != '' && !valid_date($data['finishdate']))
		{
			formError($this->lang->line('course_invalidfinishdate'), true);
		} else {
			$data['finishdate'] = format_date($data['finishdate']);
		}
		
		$data['idcandidate'] = $this->session->userdata['idprofile'];
		
		if ($this->course->get($data)->row())
		{
			formError($this->lang->line('course_courseexists'), true);
		}
		
		if ($this->course->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateCourse.getCourseGrid();closeBox();');
		}
	}
	
	public function update($id_course = '')
	{
		$data = $_POST;
		$data['id_course'] = $id_course;
		
		if (missing_field($data, array('idlevel', 'idcoursetype', 'idstatus', 'startdate', 'institution', 'idcountry')))
		{
			formError($this->lang->line('course_missingfield'), true);
		}
		
		if (!valid_date($data['startdate']))
		{
			formError($this->lang->line('course_invalidstartdate'), true);
		}
		else
		{
			$data['startdate'] = format_date($data['startdate']);
		}
		
		if ($data['idstatus'] == 1 && isset($data['finishdate']) && $data['finishdate'] == '')
		{
			formError($this->lang->line('course_missingfinishdate'), true);
		}
		
		if (isset($data['finishdate']) && $data['finishdate'] != '' && !valid_date($data['finishdate']))
		{
			formError($this->lang->line('course_invalidfinishdate'), true);
		}
		else
		{
			$data['finishdate'] = format_date($data['finishdate']);
		}
		
		$data['idcandidate'] = $this->session->userdata['idprofile'];
		
		if ($this->course->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateCourse.getCourseGrid();closeBox();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('course_missingfield'));
		}
		
		if ($this->course->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CandidateCourse.getCourseGrid();');
		}
	}
	
	public function edit($id_course = '')
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		$data['id_course'] = $id_course;
		
		$data['startdate'] = '';
		$data['finishdate'] = '';
		$data['institution'] = '';
		
		if (is_numeric($id_course))
		{
			$course_data = $this->course->get(array('idcourse' => $id_course))->row();
			
			$data['startdate'] = format_date($course_data->startdate, true);
			$data['finishdate'] = format_date($course_data->finishdate, true);
			$data['institution'] = $course_data->institution;
			$data['set_level_value'] = '$("#idlevel").val("'.$course_data->idlevel.'");';
			$data['extrascript']  = '$("#idcoursetype").val("'.$course_data->idcoursetype.'");';
			$data['extrascript'] .= '$("#idstatus").val("'.$course_data->idstatus.'");';
			$data['extrascript'] .= '$("#idcountry").val("'.$course_data->idcountry.'");';
			$data['extrascript'] .= '$("#idstate").val("'.$course_data->idstate.'");';
		}
		
		$data['userdata'] = $this->session->userdata;
        $this->load->view('candidate/course', $data);
	}
	
	public function add()
	{
		$this->edit();
	}
}

?>